
import { validFeeds } from '@/common/api';
import {
  createRouter,
  createMemoryHistory,
  createWebHistory,
  RouterOptions
} from 'vue-router';

// 是否是服务端渲染
const isServer = typeof window === 'undefined';
const history = isServer ? createMemoryHistory() : createWebHistory();
const valids: { [key: string]: any } = validFeeds;
const routerConfig: RouterOptions = {
  routes: [
    {
      path: '/',
      redirect: '/news'
    },
    {
      path: '/:feed/:page(\\d+)?',
      name: 'feed-page',
      component: () =>
        import('@/pages/FeedList.vue'),
      props: (route) => ({
        feed: route.params.feed,
        page: Number(route.params.page) || 1,
        maxPage: valids[route.params.feed.toString()].pages
      })
    },
    {
      path: '/user/:id?',
      name: 'user-id',
      component: () =>
        import('@/pages/UserPage.vue')
    },
    {
      path: '/item/:id?',
      name: 'item-id',
      component: () =>
        import('@/pages/ItemPage.vue')
    }
  ],
  history: history
};

export function _createRouter() {
  return createRouter(routerConfig);
}
